package com.unionftech.common.aspectj

import com.alibaba.android.arouter.launcher.ARouter
import com.unionftech.common.router.RouterPath
import com.unionftech.common.router.service.ILoginService
import com.unionftech.common.utils.LogUtils
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.reflect.MethodSignature

/**
 * 此注解描述的类为切面类，其内部可以定义切入点表达式和通知方法
 */
@Aspect
class LoginFilterAspect {
    @Pointcut("execution(@com.unionftech.common.aspectj.LoginFilter * *(..))")
    fun loginFilterPointCut() {
    }

    @Around("loginFilterPointCut()")
    @Throws(Throwable::class)
    fun aroundLoginPoint(joinPoint: ProceedingJoinPoint): Any? {
        var result: Any? = null
        val methodSignature = joinPoint.signature as MethodSignature
        //获取方法所属的类名
        val className = methodSignature.declaringType.simpleName
        //获取方法名
        val methodName = methodSignature.name
        LogUtils.d(TAG, "执行${className}.${methodName}之前进行登录检查-------->")
        val navigation = ARouter.getInstance().build(RouterPath.LoginServiceInfo.PATH).navigation()
        if (navigation != null) {
            LogUtils.d(TAG, "拦截判断类不为空")
            val service = navigation as ILoginService
            val isLogin = service.checkLogin()
            if (isLogin) {
                LogUtils.d(TAG, "用户已登录")
                result = joinPoint.proceed()
            } else {
                LogUtils.d(TAG, "用户未登录")
                service.gotoLogin()
            }
        }
        return result
    }

    companion object {
        private val TAG = LoginFilterAspect::class.java.simpleName
    }
}